﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentNHibernate.Mapping;
using YasharEl.Infrastructure.PersistenceModel.NHibernate.UserTypes;
using YasharEl.Infrastructure.ProcessEngine.EntityModel;
using YasharEl.Infrastructure.ProcessEngine.EntityModel.Prototype;
using YasharEl.Infrastructure.ProcessEngine.Prototype;

namespace YasharEl.Infrastructure.ProcessEngine.PersistenceModel.NHibernate.Mappings
{
    public class ProcessEntityMap : ClassMap<ProcessEntity>
    {
        public ProcessEntityMap()
        {
            Table("Processes");

            Id(e => e.ProcessId)
                .Column("ProcessId")
                    .GeneratedBy
                        .Identity();

            Map(e => e.WorkflowInstanceId)
                .Column("WorkflowInstanceId")
                .Unique()
                .Not.Nullable();

            Map(e => e.CreatedDate)
                .Column("CreatedDate")
                .Not.Nullable();

            Map(e => e.ProcessStatus)
                .Column("ProcessStatus")
                .Not.Nullable()
                .CustomType<EnumerationType<ProcessStatus>>();

            Map(e => e.ProcessPriority)
                .Column("ProcessPriority")
                .Not.Nullable()
                .CustomType<EnumerationType<ProcessPriority>>();

            Map(e => e.ExpiredDate)
                .Column("ExpiredDate")
                .Not.Nullable();

            Map(e => e.ModifiedDate)
                .Column("ModifiedDate")
                .Nullable();

            Map(e => e.StartedDate)
                .Column("StartedDate")
                .Nullable();

            Map(e => e.ClosedDate)
                .Column("ClosedDate")
                .Nullable();

            References(e => e.ClosedBy)
                .Column("ClosedBy")
                .Nullable()
                .Class<ProcessParticipantEntity>()
                .LazyLoad();

            References(e => e.ProcessDefinition)
                .Column("ProcessDefinitionId")
                .Not.Nullable()
                .Class<ProcessDefinitionEntity>()
                .LazyLoad();

            References(e => e.CreatedBy)
                .Column("CreatedBy")
                .Not.Nullable()
                .Class<ProcessParticipantEntity>()
                .LazyLoad();

            References(e => e.ModifiedBy)
                .Column("ModifiedBy")
                .Nullable()
                .Class<ProcessParticipantEntity>()
                .LazyLoad();

            References(e => e.Owner)
                .Column("Owner")
                .Not.Nullable()
                .Class<ProcessParticipantEntity>()
                .LazyLoad();

            HasMany<ProcessPropertyInstanceEntity>(e => e.Properties)
                .Not.Inverse()
                .LazyLoad()
                .Cascade.All()
                .KeyColumn("PrototypedObjectId")
                .Where
                (
                    "PrototypedObjectType = " + ProcessObjectType.Process.Value.ToString()
                );
        }
    }
}
